/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * viewReceipt.java
 *
 * Created on Oct 24, 2010, 9:02:07 PM
 */

package CAT200;
import java.sql.*;
import java.util.*;

/**
 *
 * @author ken
 */
public class viewReceipt extends javax.swing.JFrame {

    private Vector<Vector<String>> receiptData;
    private Vector<String> receiptDetail;
    /** Creates new form viewReceipt */
    public viewReceipt() {
        initComponents();
    }

    public viewReceipt(String orderNum)
    {
        try
        {
            receiptData = getInvoiceTable(orderNum);

            receiptDetail = new Vector<String>();
            receiptDetail.add("No.");
            receiptDetail.add("Product code");
            receiptDetail.add("Unit");
            receiptDetail.add("Price");

            initComponents();
            setLocationRelativeTo(null);
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        }
        catch(Exception e){  }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        CLOSE = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            receiptData, receiptDetail
        ));
        jScrollPane1.setViewportView(jTable1);

        CLOSE.setText("Close");
        CLOSE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CLOSEActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(206, 206, 206)
                        .addComponent(CLOSE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(CLOSE)
                .addGap(196, 196, 196))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                        

    private void CLOSEActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
        this.dispose();
    }                                     

    public Vector getInvoiceTable(String orderNum)throws Exception
    {
        String query = ("select LINE_NUMBER, PRODUCT_CODE, LINE_UNIT, P_PRICE from ORDERLINE where ORDER_NUMBER = '" + orderNum + "'");
        Vector<Vector<String>> invoice = new Vector<Vector<String>>();
        DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/CAT200-DB", "user", "123");

        Statement stmt = con.createStatement();
        ResultSet rset = stmt.executeQuery(query);

        while(rset.next())
        {
            Vector<String> invoiceLine = new Vector<String>();
            invoiceLine.addElement(rset.getString("LINE_NUMBER"));
            invoiceLine.addElement(rset.getString("PRODUCT_CODE"));
            invoiceLine.addElement(rset.getString("LINE_UNIT"));
            invoiceLine.addElement(rset.getString("P_PRICE"));
            invoice.add(invoiceLine);
        }

        con.close();
        stmt.close();
        rset.close();
        return invoice;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try
                {
                new viewReceipt("100001").setVisible(true);
                }
                catch(Exception e){ }
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton CLOSE;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                   

}
